import Taro from "@tarojs/taro";
import { RoleType } from "types/user";

export async function GetUserOpenId() {
    const { errMsg, code } = await Taro.login()
    if (!code) return Taro.showModal({
        title: '授权失败',
        content: JSON.stringify(errMsg)
    })

    return Taro.request({
        method: 'POST',
        url: '/api/user/openid',
        data: { code } // 传递code
    }).then(res => res?.data)
}

export function GetUserInfo() {
    return Taro.request({
        url: `/api/user/info`,
        method: 'GET'
    }).then<UserInfo>(res => res?.data)
}



export type UserStatus = 1 | 0 | 2 | 3 // ? 状态：1=正常，0=待审核，2=未通过，3=禁用
export type UserInfo = {
    role: RoleType
    id: number
    token: string
    create_time: string
    update_time: string
    status: UserStatus
    headimg: string
    name: string
    phone: string
    reg_code: string
    openid: string
    refer_code: string
    reg_name: string
    company: string
    beans: number // ?帮豆
    points: number // ?积分
    left_points: number // ?剩余可兑换
    max_points: number // ?本月最多可兑换
    tx_points: number // ?已提现积分
    tags: string
    charging: boolean // ?是否有充值订单审批中
    charging_beans: number
    dj_points: number // ?冻结的积分
    vip: 0 | 1 | 2  // ? 1-金卡 2-银卡
    vip_time: string
    invite_status: string
    vip_end_time: string
    last_vip: string
    point_tx: boolean // ?是否可提现
    txing_points: number // ?正在提现审核中的积分
}

// 600 未登录
// 601 审核中
// 602 审核未通过
// 603 禁用
export function UserLoginByRole(params: {
    role: RoleType
    code: string
    reg_code?: string
}) {
    return Taro.request({
        url: `/api/user/role`,
        method: 'GET',
        data: params
    }).then<UserInfo>(res => res?.data)
}


export function UserRegisterInfo(params: {
    headimg?: string
    name: string
    company: string
    reg_code?: string
    reg_name?: string
}) {
    return Taro.request({
        url: `/api/user/data`,
        method: 'POST',
        data: params
    })
}


// ?退出登录
export function UserLoginout() {
    return Taro.request({
        url: `/api/user/logout`,
        method: 'POST',
    })
}

// ?帮豆记录
export function MoneyBeans(params: {
    type: '1' | '2'
}) {
    return Taro.request({
        url: `/api/money/beans`,
        method: 'POST',
        data: params
    }).then<{
        create_time: string
        biz: number
        biz_name: string
        id: number
        is_sync: 1 | 2
        money: number
        money_after: number
        money_before: number
        msg: string
        ref: number
        type: 1 | 2
        unit: number
        user_id: number
    }[]>(res => res?.data)
}

// ?积分操作记录
export function MoneyPoints(params: {
    type: '1' | '2'
}) {
    return Taro.request({
        url: `/api/money/points`,
        method: 'POST',
        data: params
    }).then<{
        create_time: string
        biz: number
        biz_name: string
        id: number
        is_sync: 1 | 2
        money: number
        money_after: number
        money_before: number
        msg: string
        ref: number
        type: 1 | 2
        unit: number
        user_id: number
        tx_status: 0 | 1 | 2
    }[]>(res => res?.data)
}

// ?邀请记录
export function UserInvites() {
    return Taro.request({
        url: `/api/user/invites`,
        method: 'GET',
    }).then<{
        dj_points: number
        total: number
        total_points: number
        list: {
            invite_status: string
            create_time: string
            id: number
            msg: string
            re_time: string
            re_uid: number
            refer_code: string
            refer_uid: number
            remark: string
            status: 1 | 2 | 3 | 0 // ?0=审核中，1=冻结，2=成功，3=作废'
            user: UserInfo,
            user_id: number
        }[]
    }>(res => res?.data)
}


// ?生成海报
export function UserSharePoster(params: {
    page: string
    scene: string // ?https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/qrcode-link/qr-code/getUnlimitedQRCode.html
}) {
    return Taro.request({
        method: 'POST',
        url: '/api/user/share',
        data: params
    }).then(res => res?.data)
}


// ?根据推荐码获取用户头像
export function UserShareInfo(params: {
    code: string
}) {
    return Taro.request({
        method: 'GET',
        url: '/api/user/shareInfo',
        data: params
    }).then<{
        img: string
        name: string
        openid: string
    }>(res => res?.data)
}

// ?会员信息
export function UserVip() {
    return Taro.request({
        method: 'GET',
        url: '/api/user/vips',
    }).then<{
        applying: boolean
        list: {
            beans: number
            days: number
            expired_days: number
            id: number
            info: string
            name: string
            point_tx: boolean
            point_tx_limit: number
            price: number
            recommend: boolean
        }[]
    }>(res => res?.data)
}


// ?申请开通会员
export function ApplyVip(params: {
    vip: number
}) {
    return Taro.request({
        method: 'POST',
        url: '/api/user/applyVip',
        data: params
    }).then(res => res?.data)
}


// ?积分提现
export function MoneyTxPoints(params: {
    points: number
}) {
    return Taro.request({
        method: 'POST',
        url: '/api/money/txPoints',
        data: params
    }).then(res => res?.data)
}